From 073f8bc44bd5fae222d1d94cba31c872a8502826 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 31 Jan 2016 12:57:50 -0500 Subject: [PATCH] gtk-builder-tool: Expand the preview command Make the preview command parse options properly, turn the ID into an --id=ID option, and add a --css=FILE option that allows to specify a css file to use for previewing. This is useful for e.g. previewing the reftest .ui files with their corresponding .css. --- docs/reference/gtk/gtk-builder-tool.xml | 22 ++++- gtk/gtk-builder-tool.c | 116 ++++++++++++++++++------ 2 files changed, 105 insertions(+), 33 deletions(-) diff --git a/docs/reference/gtk/gtk-builder-tool.xml b/docs/reference/gtk/gtk-builder-tool.xml index be8113bccc..375cdb967b 100644 --- a/docs/reference/gtk/gtk-builder-tool.xml +++ b/docs/reference/gtk/gtk-builder-tool.xml @@ -31,6 +31,7 @@ gtk-builder-tool COMMAND +OPTION FILE @@ -63,9 +64,24 @@ Lists all the named objects that are created in the .ui file. - ID - Preview the object with the given ID. If ID is not specified, - try to find a suitable object to preview. + + Preview the .ui file. This command accepts options + to specify the ID of an object and a .css file to use. + + + + +Preview Options + The command accepts the following options: + + + + The ID of the object to preview. If not specified, + gtk-builder-tool will choose a suitable object on its own. + + + + Load style information from the given .css file. diff --git a/gtk/gtk-builder-tool.c b/gtk/gtk-builder-tool.c index bc18b38fe7..8a42e4b262 100644 --- a/gtk/gtk-builder-tool.c +++ b/gtk/gtk-builder-tool.c @@ -799,14 +799,31 @@ set_window_title (GtkWindow *window, } static void -do_preview (const char *filename, - const char *id) +preview_file (const char *filename, + const char *id, + const char *cssfile) { GtkBuilder *builder; GError *error = NULL; GObject *object; GtkWidget *window; + if (cssfile) + { + GtkCssProvider *provider; + + provider = gtk_css_provider_new (); + if (!gtk_css_provider_load_from_path (provider, cssfile, &error)) + { + g_printerr ("%s\n", error->message); + exit (1); + } + + gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), + GTK_STYLE_PROVIDER (provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + } + builder = gtk_builder_new (); if (!gtk_builder_add_from_file (builder, filename, &error)) { @@ -885,6 +902,53 @@ do_preview (const char *filename, g_object_unref (builder); } +static void +do_preview (int *argc, + const char ***argv) +{ + GOptionContext *context; + char *id = NULL; + char *css = NULL; + char **filenames = NULL; + const GOptionEntry entries[] = { + { "id", 0, 0, G_OPTION_ARG_STRING, &id, NULL, NULL }, + { "css", 0, 0, G_OPTION_ARG_FILENAME, &css, NULL, NULL }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL } + }; + GError *error = NULL; + + context = g_option_context_new (NULL); + g_option_context_set_help_enabled (context, FALSE); + g_option_context_add_main_entries (context, entries, NULL); + + if (!g_option_context_parse (context, argc, (char ***)argv, &error)) + { + g_printerr ("%s\n", error->message); + g_error_free (error); + exit (1); + } + + g_option_context_free (context); + + if (filenames == NULL) + { + g_printerr ("No .ui file specified\n"); + exit (1); + } + + if (g_strv_length (filenames) > 1) + { + g_printerr ("Can only preview a single .ui file\n"); + exit (1); + } + + preview_file (filenames[0], id, css); + + g_strfreev (filenames); + g_free (id); + g_free (css); +} + static void usage (void) { @@ -892,17 +956,21 @@ usage (void) " gtk-builder-tool [COMMAND] FILE\n" "\n" "Commands:\n" - " validate Validate the file\n" - " simplify Simplify the file\n" - " enumerate List all named objects\n" - " preview [ID] Preview the named object\n" + " validate Validate the file\n" + " simplify Simplify the file\n" + " enumerate List all named objects\n" + " preview [OPTIONS] Preview the file\n" + "\n" + "Preview Options:\n" + " --id=ID Preview only the named object\n" + " --css=FILE Use style from CSS file\n" "\n" "Perform various tasks on GtkBuilder .ui files.\n")); exit (1); } int -main (int argc, char *argv[]) +main (int argc, const char *argv[]) { g_set_prgname ("gtk-builder-tool"); @@ -916,29 +984,17 @@ main (int argc, char *argv[]) if (strcmp (argv[2], "--help") == 0) usage (); - if (strcmp (argv[1], "validate") == 0) - do_validate (argv[2]); - else if (strcmp (argv[1], "simplify") == 0) - do_simplify (argv[2]); - else if (strcmp (argv[1], "enumerate") == 0) - do_enumerate (argv[2]); - else if (strcmp (argv[1], "preview") == 0) - { - const char *filename, *id; - - if (argc > 3) - { - id = argv[2]; - filename = argv[3]; - } - else - { - id = NULL; - filename = argv[2]; - } - - do_preview (filename, id); - } + argv++; + argc--; + + if (strcmp (argv[0], "validate") == 0) + do_validate (argv[1]); + else if (strcmp (argv[0], "simplify") == 0) + do_simplify (argv[1]); + else if (strcmp (argv[0], "enumerate") == 0) + do_enumerate (argv[1]); + else if (strcmp (argv[0], "preview") == 0) + do_preview (&argc, &argv); else usage (); -- 2.30.2